package com.open.rbac.controller.sys;

import java.text.ParseException;

import com.nimbusds.jose.JOSEException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.open.center.common.model.CommonResult;
import com.open.center.jwt.exception.JwtExpiredException;
import com.open.rbac.model.UserInfo;
import com.open.rbac.model.vo.sys.LoginVo;
import com.open.rbac.service.sys.ILoginService;


/**
 * 登录控制器
 *
 * @author Riche's
 * @since 2022/8/15
 */
@Tag(name = "登录接口", description = "登录接口控制器")
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功",
                content = {@Content(schema = @Schema(implementation = UserInfo.class))}),
})
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1")
public class LoginController {

    private final ILoginService loginService;

    @Operation(summary = "登录")
    @PostMapping("/login")
    public CommonResult<UserInfo> login(@RequestBody @Valid LoginVo loginVo) {
        return CommonResult.success(loginService.login(loginVo));
    }

    @Operation(summary = "验证Token")
    @GetMapping("/verify_token")
    public CommonResult<UserInfo> verify(@RequestParam String token)
            throws JwtExpiredException, ParseException, JOSEException {
        return CommonResult.success(loginService.verify(token));
    }

    @Operation(summary = "刷新Token")
    @GetMapping("/refresh_token")
    public CommonResult<UserInfo> refresh(@RequestParam String token)
            throws JwtExpiredException, ParseException, JOSEException {
        return CommonResult.success(loginService.refresh(token));
    }

}
